﻿
Imports jmail

Module email

    Public Structure JMailMessage
        Dim SmtpServer As String
        Dim SmtpUser As String
        Dim SmtpPassWord As String

        Dim MailFrom As String
        Dim MailFromName As String
        Dim MailTo As String
        Dim MailToName As String

        Dim Subject As String
        Dim Body As String
        Dim Files As String
    End Structure

    Public Function SendMail(ByVal Mailmsg As JMailMessage) As Boolean
        'On Error Resume Next
        Dim JMail As jmail.Message
        Dim i As Integer
        Dim MailToX() As String
        Dim MailTONameX() As String
        Dim FilesX() As String

        JMail = New jmail.Message
        JMail.Charset = "gb2312" ' 邮件字符集，默认为"US-ASCII"
        ' JMail.ISOEncodeHeaders = False ' 是否进行ISO编码，默认为True

        ' 发送者信息（可用变量方式赋值）
        JMail.From = Mailmsg.MailFrom  ' 发送者地址
        JMail.FromName = Mailmsg.MailFromName  ' 发送者姓名


        ' 身份验证
        JMail.MailServerUserName = Mailmsg.SmtpUser  '身份验证的用户名
        JMail.MailServerPassWord = Mailmsg.SmtpPassWord  ' 身份验证的密码

        ' 设置优先级，范围从1到5，越大的优先级越高，3为普通
        JMail.Priority = 3


        ' 加入一个收件人【变量email：收件人地址】可以同一语句重复加入多个

        MailToX = Split(Mailmsg.MailTo, "|")
        MailTONameX = Split(Mailmsg.MailToName, "|")
        For i = 0 To UBound(MailToX)
            JMail.AddRecipient(MailToX(i), MailTONameX(i))
        Next


        JMail.Subject = Mailmsg.Subject  ' 邮件主题

        ' 邮件主体（文本部分）
        JMail.Body = Mailmsg.Body

        '附件
        FilesX = Split(Mailmsg.Files, "|")
        For i = 0 To UBound(MailToX)
            'JMail.AddAttachment(FilesX(i), True)
        Next

        ' 发送【调用格式：objJMail.Send([username:password@]SMTPServerAddress[:Port])】
        Dim Login As String = Mailmsg.SmtpUser & ":" & Mailmsg.SmtpPassWord & "@" & Mailmsg.SmtpServer
        JMail.Send(Login)

        ' 关闭并清除对象
        JMail.Close()
        JMail = Nothing
    End Function

    Public Function GetEmail(ByVal UserName As String, ByVal PassWord As String, ByVal PopServer As String, ByVal Subject As String) As Boolean
        On Error GoTo ErrorHandler

        Dim Pop3 As jmail.POP3
        Dim msg As jmail.Message
        Dim i As Integer
        Dim msgIndex As Integer

        Dim NewVersion As String
        Dim OldVersion As String = 0
        Dim swtexe As String

        Dim updatefolder As String

        '获取邮件目录，对比subject 得到 index
        Pop3 = New POP3
        Pop3.Connect(UserName, PassWord, PopServer)

        Pop3.DownloadHeaders()

        If Pop3.Count > 0 Then
            For i = 1 To Pop3.Count
                msg = Pop3.Messages.Item(i)
                If Pop3.Messages(i).Subject = Subject Then        '主题符合
                    msgIndex = i
                    Exit For
                End If
            Next
        End If
        Pop3.Disconnect()
        Pop3 = Nothing


        '重新连接，下载指定邮件
        Pop3 = New POP3
        Pop3.Connect(UserName, PassWord, PopServer)

        msg = Pop3.Messages.Item(msgIndex)

        NewVersion = msg.Body
        swtexe = My.Application.Info.DirectoryPath & "\swtool.exe"
        If My.Computer.FileSystem.FileExists(swtexe) = True Then
            OldVersion = My.Application.Info.Version.ToString
        End If

        If NewVersion > OldVersion And msg.Attachments.Count > 0 Then    '遍历每个附件

            updatefolder = My.Application.Info.DirectoryPath & "\Update\"
            If My.Computer.FileSystem.DirectoryExists(updatefolder) Then
                My.Computer.FileSystem.DeleteDirectory(updatefolder, FileIO.DeleteDirectoryOption.DeleteAllContents)
            End If
            My.Computer.FileSystem.CreateDirectory(updatefolder)

            For j = 0 To msg.Attachments.Count - 1
                Dim att As jmail.Attachment = msg.Attachments(j)
                att.SaveToFile(updatefolder & att.Name)
            Next
            Return True
        Else
            Return False
        End If

        Pop3.Disconnect()

ErrorHandler:
        Pop3 = Nothing
        Return False
    End Function

End Module


